package com.ddm.controller.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddm.entity.webEntity.Course;
import com.ddm.entity.webEntity.New;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.webService.NewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("WebNewController")
@RequestMapping("/Web/new")
@Api(tags = "新闻相关接口")
@Slf4j
public class NewController {

    @Autowired
    private NewService newService;


    @GetMapping("/findById")  // 统一改为查询参数方式
    @ApiOperation("根据id查询新闻")
    public Result<New> getById(
            @ApiParam(value = "新闻id", required = true)
            @RequestParam(required = false) Integer id  // 保持与课程接口一致的参数传递方式
    ) {
        log.info("根据id查询新闻:{}", id);
        New news = newService.getNewById(id);
        return Result.success(news);
    }

    @PostMapping
    @ApiOperation("添加新闻")
    public Result<New> add(
            @RequestBody  New news
    ) {
        Boolean result = newService.add(news);
        return result ? Result.success(news) : Result.error("添加失败");  // 统一返回逻辑
    }

    @PutMapping
    @ApiOperation("更新新闻")
    public Result<New> update(@RequestBody New news) {
        if (news.getId() == null) {
            return Result.error("更新失败，id不能为空");
        }
        newService.update(news);
        return Result.success(news) ;  // 细化错误信息
    }

    @DeleteMapping("/delete")  // 统一改为查询参数方式
    @ApiOperation("删除新闻")
    public Result<New> delete(
            @ApiParam(value = "新闻id", required = true)
            @RequestParam(required = false) Integer id  // 保持与课程接口一致的参数传递方式
    ) {
        New deletedNew = newService.getNewById(id);  // 先查询再删除，确保返回删除前的数据
        newService.delete(id);
        return Result.success(deletedNew);
    }

    @GetMapping("/pageAll")
    @ApiOperation("分页查询新闻信息")
    public Result<PageResult> getNewsByPage(
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,

            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String key
    ) {
        Page<New> page = new Page<>(pageNum, pageSize);
        IPage<New> newsPage = newService.getNewByPage(page,key);

        return Result.success(new PageResult(
                newsPage.getTotal(),
                newsPage.getRecords()
        ));
    }
//    @GetMapping("/page")
//    @ApiOperation("根据分类ID分页查询新闻信息")
//    public Result<PageResult> getNewsByCategoryIds(
//            @ApiParam(value = "分类ID列表（多个ID用 &ids= 拼接）", required = true)
//            @RequestParam("ids") List<Integer> ids, // ✅ 使用 ids 作为参数名
//            Integer pageNum,
//            Integer pageSize
//    ) {
//        log.info("根据分类ID分页查询新闻信息，分类ID：{}，页码：{}，每页大小：{}",
//                ids, pageNum, pageSize);
//
//        Page<New> page = new Page<>(pageNum, pageSize);
//        // 调用服务层方法（方法名保持一致，参数名改为 ids）
//        IPage<New> newsPage = newService.getNewsByIds(ids, page);
//
//        PageResult pageResult = new PageResult(
//                newsPage.getTotal(),
//                newsPage.getRecords()
//        );
//
//        return Result.success(pageResult);
//    }
}